package com.ophiux.cs.infrastructure.repositories.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ophiux.cs.domain.user.model.entity.UserLoginRecore;
import com.ophiux.cs.domain.user.repositories.IUserRecoreRepository;
import com.ophiux.cs.infrastructure.compoment.basedata.mybatis.DataComponent;
import com.ophiux.cs.infrastructure.mapper.user.UserRecoreMapper;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserRecoreRepository implements IUserRecoreRepository{
	@Autowired
	UserRecoreMapper userRecoreMapper;
	
	@Autowired
	DataComponent dataComponent;
	
	@Override
	@Transactional//开启事务回滚
	public boolean loadUserRecore(UserLoginRecore recore)  throws OphiuxRepositoryException{
		boolean loadResult = false ;
		try {
			userRecoreMapper.addUserRecore(recore);
//			dataComponent.insertData(tag+"addUserRecore", recore);
			loadResult = true;
		} catch (Exception e) {
			log.info("loadUserRecore仓储异常");
			e.printStackTrace();
			throw new OphiuxRepositoryException("loadUserRecore仓储异常",e);
		}
		return loadResult;
	}

	@Override
	@Transactional//开启事务回滚
	public UserLoginRecore getUserRecoreByUserId(String userId)  throws OphiuxRepositoryException{
		UserLoginRecore recore = null;
		try {
			recore = userRecoreMapper.getUserRecoreByUserId(userId);
//			recore = (UserLoginRecore) dataComponent.getObject(tag+"getUserRecoreByUserId", userId);
		} catch (Exception e) {
//			log.info("getUserRecoreByUserId仓储异常");
			e.printStackTrace();
			throw new OphiuxRepositoryException("getUserRecoreByUserId仓储异常",e);
		}
		return recore;
	}

	@Override
	public boolean upUserRecoreByUserId(UserLoginRecore recore)  throws OphiuxRepositoryException{
		boolean upResult = false;
		try {
			userRecoreMapper.updateUserRecoreByUserId(recore);
//			dataComponent.updateData(tag+"updateUserRecoreByUserId", recore);
			upResult = true;
		} catch (Exception e) {
			log.info("upUserRecoreByUserId仓储异常");
			e.printStackTrace();
			throw new OphiuxRepositoryException("upUserRecoreByUserId仓储异常",e);
		}
		return upResult;
	}

	@Override
	public UserLoginRecore getUserRecoreByToken(String token)  throws OphiuxRepositoryException{
		UserLoginRecore recore = null;
		try {
			recore = userRecoreMapper.getUserRecoreByToken(token);
//			recore = (UserLoginRecore) dataComponent.getObject(tag+"getUserRecoreByToken", token);
		} catch (Exception e) {
			log.info("getUserRecoreByUserId仓储异常");
			e.printStackTrace();
		}
		return recore;
	}

}
